import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Material 2.15
import './../../../controls'
//缴纳押金--订单信息
Rectangle {
    anchors.fill: parent
    gradient: mainGradient
    property var orderInfo: null
    property bool popShow: false
    property var lastPW: ''
    Component.onCompleted: {

        // lvMode.append({bno: "001", checked: true} )
        // lvMode.append({bno: "002", checked: true} )

    }

    function scanerQRCode()
    {
        var request = HttpHelper.createIMRequest();
        // request.enabledLog=true
        request.onSucceed( function(request){
            console.log('----water_order_get_detail-/*-*/-',JSON.stringify(request.responseData))
            orderInfo=request.responseData
            lvMode.clear()
            for(let i=0;i<orderInfo.bracelets.length;i++)
            {
                let bracelet=orderInfo.bracelets[i]
                bracelet.checked=false
                lvMode.append(bracelet )
            }
        } );
        request.onError(  function(request){
            CGlobal.showMessage(window,request.errorMessage)
        } );
        request.get("/sys/watpc/pc/water_query_info_wno/"+lastPW)
    }

    function payCallBack(bno){
        console.log('---------pay successed------',bno)
        scanerQRCode()
    }

    function selectTypeCallBack(type,bno)
    {
        var request = HttpHelper.createIMRequest();
        // request.enabledLog=true
        request.onSucceed( function(request){
            //            console.log('----water_qr_exchange---',JSON.stringify(request.responseData))
            // var dlg=CGlobal.createObjectAndOpen(window,'qrc:/main/mamager/settlement/PayDialog.qml',
            //                                     {
            //                                         amount:parseFloat(request.responseData.deposit),
            //                                         bno:request.responseData.b_no,
            //                                         isdeposit:true,
            //                                         callBackFunc:payCallBack
            //                                     })
            // popShow=true
            // dlg.closed.connect(function onClosed()
            // {
            //     popShow=false
            // })
            // let b_no=request.responseData.b_no
            scanerQRCode()
        } );
        request.onError(  function(request){
            CGlobal.showMessage(window,request.errorMessage)
        } );
        request.get("/sys/watpc/pc/water_qr_exchange?water_ticket_id="+orderInfo.id+'&type='+type+'&b_no='+bno)
    }

    Connections{
        target: ScannerHelper
        function onRecvScannerData(data){
            if(data.startsWith('PW') && !popShow)
            {
                lastPW = data
                scanerQRCode()
            }
        }
    }

    Rectangle{
        id:rectRoot
        width: parent.width * 0.8
        height: 640
        anchors.horizontalCenter: parent.horizontalCenter
        y:100
        radius: 20
        Item {
            anchors.fill: parent
            visible: lastPW.length ===0
            Text {
                anchors.centerIn: parent
                color: '#333333'
                font.pixelSize: 24
                text: qsTr("请扫码微信端二维码")
            }
            TextField{
                id:textKey
                height: 42
                width: 300
                font.pixelSize: 14
                color: '#333333'
                focus: true
                placeholderText: qsTr('请输入乐园号')
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 30
                selectByMouse: true
                background: Rectangle{
                    border.color: '#dddddd'
                }

                Keys.onReturnPressed: {
                    btnQuery.clicked()
                }
                Keys.onEnterPressed: {
                    btnQuery.clicked()
                }
                Button{
                    id:btnQuery
                    width: 80
                    height: 40
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.left: parent.right
                    anchors.leftMargin: 10
                    focusPolicy:Qt.NoFocus
                    background: Rectangle{
                        radius: 10
                        color: '#EA9508'
                        Text {
                            anchors.centerIn: parent
                            color: '#ffffff'
                            text: qsTr("确定")
                        }
                    }
                    onClicked: {
                        if(textKey.text.length === 0)
                        {
                            CGlobal.showMessage(window,'请输入乐园号')
                            return
                        }
                        lastPW = textKey.text
                        scanerQRCode()
                    }
                }
            }
        }

        Item {
            anchors.fill: parent
            visible: lastPW.length > 0
            Row{
                id:rowTitle
                leftPadding: 30
                topPadding: 30
                spacing: 30
                Column{
                    spacing: 10
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("订单编号:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 's_no' in orderInfo)
                                    {
                                        return orderInfo.s_no
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("手机号码:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'phone' in orderInfo)
                                    {
                                        return orderInfo.phone
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("支付状态:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'status' in orderInfo)
                                    {
                                        switch(orderInfo.status)
                                        {
                                        case 0:
                                            return qsTr('待支付')
                                        case 1:
                                            return qsTr('已支付')
                                        case 2:
                                            return qsTr('已使用')
                                        case 3:
                                            return qsTr('已取消')
                                        case 4:
                                            return qsTr('申请退款')
                                        case 5:
                                            return qsTr('已退款')
                                        case 6:
                                            return qsTr('已拒绝')
                                        }
                                        return qsTr('未知状态')
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("儿童票数量:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'child_num' in orderInfo)
                                    {
                                        return/* orderInfo.child_num_ex +'/'+*/ orderInfo.child_num + qsTr('张')
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }

                }
                Column{
                    spacing: 10
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("下单时间:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'created_time' in orderInfo)
                                    {
                                        return orderInfo.created_time
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("姓名:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'name' in orderInfo)
                                    {
                                        return orderInfo.name
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("支付时间:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'pay_time' in orderInfo)
                                    {
                                        var dateTime = new Date( orderInfo.pay_time * 1000)
                                        return Qt.formatDateTime(dateTime, "yyyy/MM/dd HH:mm:ss");
                                        // return  orderInfo.pay_time
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                }
                Column{
                    spacing: 10
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("订单金额:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'price' in orderInfo)
                                    {
                                        return orderInfo.price
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("身份证:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'id_card' in orderInfo)
                                    {
                                        return orderInfo.id_card
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("成人票数量:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'adu_num' in orderInfo)
                                    {
                                        return /*orderInfo.adu_num_ex +'/'+*/ orderInfo.adu_num + qsTr('张')
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                }
            }
            Rectangle{
                anchors.fill: parent
                anchors.topMargin: rowTitle.height + 50
                anchors.leftMargin: 30
                anchors.bottomMargin: 60
                anchors.rightMargin: 30
                border.color: '#dddddd'
                Text {
                    text: qsTr("手环列表")
                    anchors.bottom: parent.top
                    anchors.bottomMargin: 12
                }

                Item {
                    id:itemHeader
                    width: parent.width
                    height: 30
                    property var itemWidth: (width - 60) / repHeader.model.length
                    Row{
                        Rectangle{
                            width: 60
                            height: itemHeader.height
                            border.color: '#dddddd'
                        }

                        Repeater{
                            id:repHeader
                            model: ['手环号','手环编号','性别','绑定时间','类型','押金','押金状态','押金方式','押金时间']
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: itemHeader.height
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: modelData
                                    color: '#333333'
                                }
                            }
                        }
                    }
                }
                ListModel{
                    id:lvMode
                }

                ListView{
                    id:lv
                    anchors.fill: parent
                    anchors.topMargin: itemHeader.height
                    clip: true
                    ScrollBar.vertical: ScrollBar{}
                    model:lvMode
                    delegate: Item {
                        width: lv.width
                        height: itemHeader.height *1.5
                        enabled: model.de_status === 0
                        Row{
                            Rectangle{
                                width: 60
                                height: itemHeader.height *1.5
                                border.color: '#dddddd'
                                CheckBox{
                                    anchors.centerIn: parent
                                    width: 16
                                    height: 16
                                    indicator.width: 16
                                    indicator.height: 16
                                    checked: model.checked
                                    onCheckedChanged: model.checked = checked
                                }
                            }

                            Rectangle{
                                width: itemHeader.itemWidth
                                height: itemHeader.height *1.5
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text:{
                                        if('b_name' in model && model.b_name)
                                        {
                                            return model.b_name
                                        }
                                        return   model.b_no
                                    }
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: itemHeader.height *1.5
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: model.b_no
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: itemHeader.height *1.5
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: model.sex === 0 ? '男' : '女'
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: itemHeader.height *1.5
                                border.color: '#dddddd'
                                Text {
                                    anchors.fill: parent
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignVCenter
                                    text: model.create_time
                                    color: '#333333'
                                    clip: true
                                    wrapMode: Text.WordWrap
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: itemHeader.height *1.5
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: model.type === 0 ? qsTr('成人') : qsTr('儿童')
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: itemHeader.height *1.5
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: model.deposit
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: itemHeader.height *1.5
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: {
                                        switch(model.de_status)
                                        {
                                        case 0:
                                            return qsTr('待支付')
                                        case 1:
                                            return qsTr('已支付')
                                        case 2:
                                            return qsTr('已退款')
                                        case 3:
                                            return qsTr('已取消')
                                        }
                                        return model.de_status
                                    }

                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: itemHeader.height *1.5
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: {
                                        if(model.de_status === 0)
                                        {
                                            return ''
                                        }

                                        switch(model.deposit_pay_way)
                                        {
                                        case 0:
                                            return qsTr('微信')
                                        case 1:
                                            return qsTr('支付宝')
                                        case 2:
                                            return qsTr('现金')
                                        }
                                        return model.deposit_pay_way
                                    }
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: itemHeader.height *1.5
                                border.color: '#dddddd'
                                Text {
                                    anchors.fill: parent
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignVCenter
                                    clip: true
                                    wrapMode: Text.WordWrap
                                    text: {
                                        if('pay_time' in model && model.pay_time)
                                        {
                                            return model.pay_time
                                        }
                                        return ''
                                    }
                                    color: '#333333'
                                }
                            }
                        }
                    }
                }
            }

            Row{
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 10
                spacing: 30
                Button{
                    width: 160
                    height: 40
                    enabled:  orderInfo !== null
                    background: Rectangle{
                        radius: 10
                        color: '#EA9508'
                        Text {
                            anchors.centerIn: parent
                            color: '#ffffff'
                            text: qsTr("添加手环")
                        }
                    }
                    onClicked: {
                        CGlobal.createObjectAndOpen(window,'qrc:/main/InputBraceletNoDialog.qml',
                                                    {
                                                        isAddPage:false,
                                                        qmlPath:'qrc:/main/mamager/settlement/SelectBraceleTypeDialog.qml',
                                                        properties:{callBack:selectTypeCallBack}
                                                    })
                        //CGlobal.createObjectAndOpen(window,'qrc:/main/mamager/settlement/SelectBraceleTypeDialog.qml',{callBack:selectTypeCallBack})
                    }
                }
                Button{
                    width: 160
                    height: 40
                    enabled:  orderInfo !== null
                    background: Rectangle{
                        radius: 10
                        color: '#EA9508'
                        Text {
                            anchors.centerIn: parent
                            color: '#ffffff'
                            text: qsTr("支付押金")
                        }
                    }
                    onClicked: {
                        let bnos=[]
                        let samount=0.0
                        for(let i=0;i<lvMode.count;i++)
                        {
                            let item=lvMode.get(i)
                            if(item.checked)
                            {
                                bnos.push(item)
                                samount+=parseFloat(item.deposit)
                            }
                        }
                        if(bnos.length === 0)
                        {
                            CGlobal.showMessage(window,qsTr('请勾选要支付的手环'))
                            return
                        }
                        let bnoStr=''
                        let bnameStr=''
                        for(let i=0;i<bnos.length;i++)
                        {
                            bnoStr+=bnos[i].b_no
                            bnameStr+=bnos[i].b_name
                            if(i < bnos.length - 1)
                            {
                                bnoStr+=','
                                bnameStr+=','
                            }
                        }
                        var dlg=CGlobal.createObjectAndOpen(window,'qrc:/main/mamager/settlement/PayDialog.qml',
                                                            {
                                                                amount:samount,
                                                                bno:bnoStr,
                                                                bname:bnameStr,
                                                                isdeposit:true,
                                                                callBackFunc:payCallBack
                                                            })
                        popShow=true
                        dlg.closed.connect(function onClosed()
                        {
                            popShow=false
                        })

                    }
                }
            }

        }
    }
}
